/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import Entities.TblSemester;
import Entities.TblSubject;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author CuongNV
 */
@ManagedBean
@RequestScoped
public class SubjectManagedBean extends TblSubject {

    /** Creates a new instance of SubjectManagedBean */
    public SubjectManagedBean() {
    }
    private List<TblSubject> all;
    private List<TblSubject> listSelectedSubject;
    String subId;
    private TblSubject subjectBean;

    public void laySubject(ValueChangeEvent e) {
        //assign new value to localeCode
        subId = e.getNewValue().toString();
        listSelectedSubject = getSubjectBySemesterId(Integer.parseInt(subId));
    }

    public List<TblSubject> getAll() {
        return new BLL.SubjectBLL().getAll();
    }

    public String add() {
        try {
            new BLL.SubjectBLL().add(this.subjectBean);
            return "subjectlist";
        } catch (Exception ex) {
            Logger.getLogger(BranchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }

    public String update() {
        if (new BLL.SubjectBLL().update(this.subjectBean)) {
            return "subjectlist";
        } else {
            return "null";
        }
    }
    public String delete() throws Exception{
        if (new BLL.SubjectBLL().delete(this.subjectBean)) {
            return "subjectlist";
        } else {
            return "null";
        }
    }
    public String semesterNameById(int semsId){
        TblSemester objSems=new BLL.SemesterBLL().getById(semsId);
        return objSems.getSemesterName();
    }
    public List<TblSubject> getSubjectBySemesterId(int semesterId) {
        return new BLL.SubjectBLL().getSubjectBySemesterId(semesterId);
    }

    public List<TblSubject> getListSelectedSubject() {
        return listSelectedSubject;
    }

    public void setListSelectedSubject(List<TblSubject> listSelectedSubject) {
        this.listSelectedSubject = listSelectedSubject;
    }

    public String getSubId() {
        return subId;
    }

    public void setSubId(String subId) {
        this.subId = subId;
    }

    public TblSubject getSubjectBean() {
        return subjectBean;
    }

    public void setSubjectBean(TblSubject subjectBean) {
        this.subjectBean = subjectBean;
    }
}
